package com.cyber.parkingremotev2;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.util.EncodingUtils;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class FileService extends Service {
	public static final int M_READ_OK = 11;
	public static final int M_WRITE_OK = 12;
	public static final int M_IO_ERROR = 13;
	public static final String M_DATA = "data";
	public static final String M_ERROR = "error";
	Handler mUiHandler;
	FileBinder mBinder;

	@Override
	public IBinder onBind(Intent arg0) {
		return mBinder;
	}

	public void onCreate() {
		mUiHandler = null;
		mBinder = new FileBinder();
	}

	public void setUiHandler(Handler handler) {
		mUiHandler = handler;
	}

	// 写数据到SD中的文件
	public void writeFileSdcardFile(String fileName, String write_str)
			throws IOException {
		FileOutputStream fout = new FileOutputStream(fileName);
		byte[] bytes = write_str.getBytes();

		fout.write(bytes);
		fout.close();
	}

	// 读SD中的文件
	public String readFileSdcardFile(String fileName) throws IOException {
		String res = "";
		FileInputStream fin = new FileInputStream(fileName);

		int length = fin.available();

		byte[] buffer = new byte[length];
		fin.read(buffer);

		res = EncodingUtils.getString(buffer, "UTF-8");

		fin.close();
		
		return res;
	}

	void readFile(String fileName) {
		ReadThread rt = new ReadThread(fileName);
		rt.start();
	}

	void writeFile(String fileName, String data) {
		WriteThread wt = new WriteThread(fileName, data);
		wt.start();
	}

	public class FileBinder extends Binder {
		void readFile(String fileName) {
			FileService.this.readFile(fileName);
		}

		void writeFile(String fileName, String data) {
			FileService.this.writeFile(fileName, data);
		}

		void setUiHandler(Handler handler) {
			FileService.this.setUiHandler(handler);
		}
	}

	protected void sendMessage(int msg_type, String msg_name, String msg_content) {
		if (mUiHandler != null) {
			Bundle bundle = new Bundle();
			bundle.putString(msg_name, msg_content);
			Message msg = mUiHandler.obtainMessage(msg_type);
			msg.setData(bundle);
			mUiHandler.sendMessage(msg);
		}
	}

	private class ReadThread extends Thread {
		String mFileName;

		public ReadThread(String fileName) {
			mFileName = fileName;
		}

		public void run() {
			try {
				String data = FileService.this.readFileSdcardFile(mFileName);
				sendMessage(M_READ_OK, M_DATA, data);
			} catch (IOException e) {
				sendMessage(M_IO_ERROR, M_ERROR, e.toString() + e.getMessage());
			}

		}
	}

	private class WriteThread extends Thread {
		String mFileName;
		String mData;

		public WriteThread(String fileName, String data) {
			mFileName = fileName;
			mData = data;
		}

		public void run() {
			try {
				FileService.this.writeFileSdcardFile(mFileName, mData);
				sendMessage(M_WRITE_OK, "", "");
			} catch (IOException e) {
				sendMessage(M_IO_ERROR, M_ERROR, e.getMessage());
			}

		}
	}

}
